# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import json

class SspaiPipeline:
    def open_spider(self,spider):
        if spider.name == 'sspaituijian':
            self.file= open('sspaituijian.json','w',encoding='utf8')
        else:
            self.file= open('gaoqingfm.json','w',encoding='utf8')

    def close_spider(self,spider):
        self.file.close()


    def process_item(self, item, spider):
        try:
            line = json.dumps(ItemAdapter(item).asdict(),ensure_ascii=False) + "\n"
            self.file.write(line)
            return item
        except:
            raise DropItem(f"Duplicate item found: {item!r}")

import sqlite3


class Sqlite3Pipeline(object):

    def __init__(self, sqlite_file, sqlite_table):
        self.sqlite_file = sqlite_file
        self.sqlite_table = sqlite_table
        
    @classmethod
    def from_crawler(cls, crawler):
        return cls(
            sqlite_file = crawler.settings.get('SQLITE_FILE'), # 从 settings.py 提取
            sqlite_table = crawler.settings.get('SQLITE_TABLE', 'items')
        )

    def open_spider(self, spider):
        self.conn = sqlite3.connect(self.sqlite_file)
        self.cur = self.conn.cursor()
        sql = 'create table if not exists [%s] ' % self.sqlite_table
        self.conn.execute(sql)
        self.conn.commit()

    def close_spider(self, spider):
        self.conn.close()

    def process_item(self, item, spider):
        insert_sql = "insert into {0}({1}) values ({2})".format(self.sqlite_table, 
                                                                ', '.join(item.fields.keys()),
                                                                ', '.join(['?'] * len(item.fields.keys())))
        self.cur.execute(insert_sql, item.fields.values())
        self.conn.commit()
        
        return item
    
